package com.controller;

import com.pojo.User;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;


import java.util.List;

@Controller("userControllerImpl")
@RequestMapping("/user")
public class UserControllerImpl implements UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/add")
    public String add(User user, Model model) {

        if (userService.add(user)) return "redirect:/login1.jsp";
        return "redirect:/register.jsp";
    }

    @RequestMapping("/update")
    public String update(User user, Model model) {

        if (userService.add(user)) return "redirect:/login1.jsp";
        return "redirect:/register.jsp";
    }

    @RequestMapping("/query")
    public String query(User user, Model model) {
        /*if(errors.hasErrors()){
            for (ObjectError e:errors.getAllErrors()) {
                System.out.println(e.getDefaultMessage());
            }
            return "redirect:/login1.jsp";
        }*/
        //System.out.println(1111);
        User user1 = userService.query(user);
        //System.out.println(222);
        model.addAttribute("user",user1);
        /*if (back == 1) return "redirect:/register.jsp";
        else if (back == 2) return "redirect:/login1.jsp";*/
        return "success";
    }
    @RequestMapping("/selectAllSalesman")
    public String selectAllSalesman(Model model) {
        List<User> allUser=userService.selectAllSalesman();
        model.addAttribute("allUser",allUser);
        return "redirect:/register.jsp";
    }
}
